<?php

namespace app\web\service;

use app\common\model\commonModel;
use app\common\util\ErrorCode;
use think\facade\Validate;

/**
 * Class Auth
 * @package app\admin\service
 * 用户联系人服务类
 */
class UserContactService
{
    private $returnData;
    private $field = [
        "name" => "require", "mobile" => "require", "prefix" => "require", "type" => "", "number" => "", "sex" => ""
    ]; //填入数据库需要存储的表字段
    private $fieldMsg = [
        "name" => "联系人姓名", "mobile" => "联系人手机号", "prefix" => "联系人手机号前缀", "number" => "身份证"
    ];
    private $data = [];

    public function __construct($data)
    {
        $this->returnData = $data;
    }

    public function getAllData($userId){
        if(empty($userId)){
            return ErrorCode::code("fail");
        }
        $data = commonModel::getList(contactTable, "id,name,prefix,mobile,type,number,sex", ["user_id" => $userId, "mall_id" => request()->mallData["id"]], 0, 0, "create_time desc", false);
        $this->returnData["data"] = $data;
        return $this->returnData;
    }

    //添加
    public function toAdd($params){
        foreach ($this->field as $k => $v) {
            $ks = parse_name($k); //转换字段，变驼峰
            if ($v === 'require' && empty($params[$ks])) {
                return ErrorCode::code("params_not_exist", $this->fieldMsg[$k]);
            }
            $this->data[$k] = isset($params[$ks]) ? $params[$ks] : $v;
        }
        $this->data["user_id"] = $params["userId"];
        $this->data["mall_id"] = request()->mallData["id"];
        $this->data["create_time"] = date("Y-m-d H:i");
        if(!Validate::isMobile($params["mobile"])){
            return ErrorCode::code("params_format", "手机号");
        }
        if(!empty($params["number"]) && !Validate::isIdCard($params["number"])){
            return ErrorCode::code("params_format", "身份证号");
        }
        if(commonModel::getCount(contactTable, ["user_id" => $params["userId"], "mobile" => $params["mobile"]])){
            return ErrorCode::code("mobile_exist");
        }
        $result = commonModel::toAdd(contactTable, $this->data);
        if(isset($result["code"])){
            return $result;
        }
        if($result) {
            return $this->returnData;
        }else{
            $this->returnData = ErrorCode::code("fail");
            $this->returnData["msg"] = "添加失败";
            return $this->returnData;
        }
    }

    //添加
    public function getDetail($params){
        if(empty($params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }
        $this->returnData["data"] = commonModel::getFields(contactTable, "*", ["id" => $params["id"]], true, false);
        return $this->returnData;
    }

    //更新
    public function toUpdate($params){
        if(empty($params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }
        foreach ($this->field as $k => $v) {
            $ks = parse_name($k); //转换字段，变驼峰
            if ($v === 'require' && empty($params[$ks])) {
                return ErrorCode::code("params_not_exist", $this->fieldMsg[$k]);
            }
            if(isset($params[$ks]))
                $this->data[$k] = $params[$ks];
        }
        if(!Validate::isMobile($params["mobile"])){
            return ErrorCode::code("params_format", "手机号");
        }
        if(!empty($params["number"]) && !Validate::isIdCard($params["number"])){
            return ErrorCode::code("params_format", "身份证号");
        }
        if(commonModel::getCount(contactTable, [["id", "<>", $params["id"]], ["mobile", "=", $params["mobile"]]])){
            return ErrorCode::code("mobile_exist");
        }
        $result = commonModel::toUpdate(contactTable, $this->data, ['id' => $params['id'], "user_id" => $params["userId"]]);
        if(isset($result["code"])){
            return $result;
        }
        if($result) {
            return $this->returnData;
        }else{
            $this->returnData = ErrorCode::code("fail");
            $this->returnData["msg"] = "更新失败";
            return $this->returnData;
        }
    }

    //删除
    public function toDel($params){
        if(empty($params['id'])){
            return ErrorCode::code("params_not_exist", "id");
        }
        $result = commonModel::delById(contactTable, $params['id']);
        if (!$result) {
            $this->returnData = ErrorCode::code("fail");
            $this->returnData["msg"] = "删除失败";
        }
        return $this->returnData;
    }
}